package cn.regexp.common.core.web;

import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;

/**
 * @author Regexpei
 * @date 2024/7/3 22:22
 * @description 统一响应结果类
 */
@Setter
@Getter
public class Result<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    public static final int SUCCESS_CODE = 200;

    public static final String SUCCESS_MESSAGE = "操作成功";

    public static final int ERROR_CODE = 500;

    public static final String ERROR_MESSAGE = "系统异常";

    /**
     * 响应码
     */
    private int code;
    /**
     * 响应消息
     */
    private String msg;

    /**
     * 响应结果
     */
    private T data;

    public Result() {}

    private Result(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static <T> Result<T> success(T data) {
        return new Result<>(SUCCESS_CODE, SUCCESS_MESSAGE, data);
    }

    public static <T> Result<T> success(String msg, T data) {
        return new Result<>(SUCCESS_CODE, msg, data);
    }

    public static <T> Result<T> error(CodeMsg codeMsg) {
        return new Result<>(codeMsg.getCode(), codeMsg.getMsg(), null);
    }

    public static <T> Result<T> defaultError() {
        return new Result<>(ERROR_CODE, ERROR_MESSAGE, null);
    }

    public boolean hasError() {
        return this.code != SUCCESS_CODE;
    }

}
